package com.gxa.service.community.impl;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.dto.communitydto.HouseNameDto;
import com.gxa.dto.communitydto.OwnerDto;
import com.gxa.dto.communitydto.OwnerHouseDto;
import com.gxa.dto.communitydto.OwnerSearchDto;
import com.gxa.entity.community.Owner;
import com.gxa.mapper.community.HouseMapper;
import com.gxa.mapper.community.OwnerMapper;
import com.gxa.service.community.OwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Service
public class OwnerServiceImpl extends ServiceImpl<OwnerMapper, Owner> implements OwnerService {

    @Autowired
    private OwnerMapper ownerMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public PageInfo<OwnerDto> queryAll(OwnerSearchDto ownerSearchDto,Integer pageNum,Integer pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<OwnerDto> ownerDtos = ownerMapper.queryAll(ownerSearchDto);
        PageInfo<OwnerDto> ownerDtoPageInfo = new PageInfo<>(ownerDtos);

        return ownerDtoPageInfo;
    }

    @Override
    public void delete(Integer id) {
        ownerMapper.deleteById(id);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,isolation = Isolation.DEFAULT,readOnly = false,timeout = -1)
    public void add(Owner owner) {
        ownerMapper.insert(owner);
        ownerMapper.addOhRelation(owner.getOwnerNum(),owner.getHouseNum(),owner.getOwnerType());


    }

    @Override
    public void addOhRelation(Owner owner) {
        ownerMapper.addOhRelation(owner.getOwnerNum(),owner.getHouseNum(),owner.getOwnerType());
    }

    @Override
    public OwnerDto queryById(Integer id) {
        OwnerDto ownerDto = ownerMapper.queryById(id);
        return ownerDto;
    }


    @Override
    @Transactional(propagation= Propagation.REQUIRED,isolation = Isolation.DEFAULT,readOnly = false,timeout = -1)
    public void update(Owner owner) {
        ownerMapper.updateById(owner);
        //根新关系表中的数据
        ownerMapper.updateOwnerType(owner.getOwnerNum(),owner.getOwnerType());
    }

    @Override
    public List<OwnerHouseDto> queryByOwnerNum(String ownerNum) {
        List<OwnerHouseDto> ownerHouseDtos = ownerMapper.queryByOwnerNum(ownerNum);
        return ownerHouseDtos;
    }

    @Override
    public List<HouseNameDto> queryHouseName() {
        List<HouseNameDto> houseNameDtos = houseMapper.queryHouseName();
        return houseNameDtos;
    }

    @Override
    public List<Owner> queryByNum(String ownerNum) {
        List<Owner> owners = ownerMapper.queryByNum(ownerNum);
        return owners;
    }
}
